<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Feiyu extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function callback()
    {        
        $request_data = $this->request->param();
        if(!empty($request_data['auth_code'])){
            db('ec_token')->where('platform','feiyu')->update([
                'auth_code'=>$request_data['auth_code']
            ]);
        }
        
        
        qqq('feiyucallback',$request_data);
        
    }
    
    //线索回调
    public function clubcallback()
    {        
        $request_data = $this->request->param();               
        qqq('clubcallback',$request_data);
//        $str = '{"gender":"未知","promotion_id":"7402546167832379455","store_pack_id":"0","req_id":"202409080727546083578C76E66A099B97","city_name":"玉林","external_url":"https:\/\/www.chengzijianzhan.cc\/tetris\/page\/7320438684993552435\/","promotion_name":"自动0813-1","remark_dict":{"您的情况（全国诚意招商）":"兼职创业"},"adv_id":"1786419710288009","mid_info":"{&quot;titleId&quot;:7280061892453515324,&quot;videoId&quot;:7261060641321123852}","id":"7412049037437698099","clue_convert_status":"合法转化","adv_name":"YPC-北京汉台梦酒业销售有限公司","store_id":"0","address":"450900","create_time":"1725752133","clue_source":"1","province_name":"广西","site_name":"贵州汉台酒业","app_name":"字节-通投智选","module_id":"1787218828311556","flow_type":"4","telphone":"18269453978","site_id":"7320438684993552435","name":"何总","clue_type":"0","location":"广西+玉林","module_name":"0105免费领酒水报价单+利润分析报表","cid":"0"}';
//        $request_data = json_decode($str,true);
        if(!empty($request_data)){
            $memo = '';
            if(!empty($request_data['remark_dict'])){
                foreach ($request_data['remark_dict'] as $key=>$value){
                    $memo.= $key .':' .$value .';';
                }
            }
            
            $save_data = [
                'clue_id'=>$request_data['id'],//线索ID
                'advertiser_id'=>$request_data['adv_id'],//广告主ID
                'name'=>$request_data['name'],//姓名
                'telphone'=>$request_data['telphone'],//电话
                'ad_id'=> empty($request_data['ad_id']) ? '' : $request_data['ad_id'],//计划ID
                'creative_id'=>$request_data['cid'],//创意id
                'site_id'=>empty($request_data['site_id']) ? '' : $request_data['site_id'],//站点ID
                'external_url'=>$request_data['external_url'],//落地页 url
                'clue_source'=>empty($request_data['clue_source']) ? '' : $request_data['clue_source'],//组件类型
                'clue_type'=>empty($request_data['clue_type']) ? '' : $request_data['clue_type'],//组件类型
                'memo'=>$memo,//备注
//                'clue_state'=>$request_data['id'],//线索状态值枚举值：1 新线索、2 有意向、3 转商机、4 无效、5 已加微信、7 待再次沟通
//                'clue_state_name'=>$request_data['id'],//线索状态名称 “clue_state”对应的名称
                'clue_create_time'=>$request_data['create_time'],//线索创建时间
                'content'=> json_encode($request_data,JSON_UNESCAPED_UNICODE),
                'createtime'=>time(),                
            ];
            db('ec_feiyu_xiansuo')->insertGetId($save_data);
        }
        echo json_encode(['code'=>0,'message'=>'success']);
        exit;
        
    }
    
    
    public function clearHistoryData(){
        echo date('Y-m-d H:i:s') . '【start】===========';
        $info = db('ec_zhongzhuan_config')->where('name','clear_history_data')->find();
        $day = 0;
        switch ($info['value']){
            case 1: // 删除三月以前的数据[删除后无法还原]
                $day = 90;
                break;
            case 2: // 删除一月以前的数据[删除后无法还原]
                $day = 30;
                break;
            case 3: // 删除半月以前的数据[删除后无法还原]
                $day = 15;
                break;
            case 4: // 删除7天以前的数据[删除后无法还原]
                $day = 7;
                break;
        }
        if(empty($day)) exit();
        $current_time = strtotime(date('Y-m-d') .' 00:00:00');
        $deadline = $current_time - $day * 86400;
        db(getBaiYingDataTable())->where('createtime','<',$deadline)->delete();
        echo date('Y-m-d H:i:s') . '【end】===========';

    }
    

}
